﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IntillisenseLib
{    
    public class PlaceholderObject : ObjectTemplete
    {
#region Properties
        private ObjectTemplete parent;

        public ObjectTemplete Parent
        {
            get { return parent; }
            set { parent = value; }
        }
        private String objName;

        public String ObjName
        {
            get { return objName; }
            set { objName = value; }
        }

        private String paraValue;

        public String ParaValue
        {
            get { return paraValue; }
            set { paraValue = value; }
        }
       
        private List<ObjectTemplete> childs;

        public List<ObjectTemplete> Childs
        {
            get { return childs; }
            set { childs = value; }
        }
#endregion        
        #region Contructor
        public PlaceholderObject()
        {
            ObjType = eObjectType.PLACEHOLDER;
            objName = string.Empty;
            childs = new List<ObjectTemplete>();
            parent = null;            
        }
        public PlaceholderObject(string objName, String value)
        {
            ObjType = eObjectType.PLACEHOLDER;
            this.objName = objName;
            childs = new List<ObjectTemplete>();
            parent = null;
            this.paraValue = value;            
        }
        public PlaceholderObject(String objName, String value, PlaceholderObject parent)
        {
            ObjType = eObjectType.PLACEHOLDER;
            this.objName = objName;
            childs = new List<ObjectTemplete>();
            this.parent = parent;        
            this.paraValue = value;
        }
        public PlaceholderObject(String objName, String value, PlaceholderObject parent, List<ObjectTemplete> childs)
        {
            ObjType = eObjectType.PLACEHOLDER;
            this.objName = objName;
            this.childs = childs;
            this.parent = parent;            
            this.paraValue = value;
        }
        #endregion
#region Public Method
        public void AddChild(ObjectTemplete child)
        {
            if (childs==null)
            {
                childs = new List<ObjectTemplete>();
            }
            childs.Add(child);
            ((PlaceholderObject)child).Parent = this;
        }
        public void RemoveChild(PlaceholderObject child)
        {
            if (childs == null)
            {
                return;
            }
            childs.Remove(child);            

        }
        public void SetName(String name)
        {
            this.ObjName = name;
        }
        public void SetParent(PlaceholderObject parent)
        {
            this.Parent = parent;
        }
        public void SetChilds(List<ObjectTemplete> childs)
        {
            this.Childs = childs;
        }       
        public PlaceholderObject FindProperties(String propertiesName)
        {
            foreach (PlaceholderObject chil in childs)
            {
                if (chil.GetParameterDisplayName() == propertiesName)
                {                    
                    return chil;
                }
                else if (chil.childs.Count > 0)
                {
                    return chil.FindProperties(propertiesName);
                }
            }
            return null;
        }
        
#endregion

        public override string GetParameterDisplayName()
        {
            return objName;
        }

        public override string GetParameterValue()
        {
            return paraValue;
        }

        public override string GetFullPath()
        {
            if (Parent==null)
            {
                return GetParameterDisplayName();
            }
            else
            { 
                return Parent.GetFullPath() + "." + GetParameterDisplayName();
            }
        }

        public override List<ObjectTemplete> GetAllChilds()
        {
            return childs;
        }
    }
}
